<template>
    <div :class="[styleFid=='subTable'?'rx-sub-center':'rx-center']" v-if="buttons.length>0" ref="myElement" :value="true">
        <template v-if="styleFid=='1'">
            <div class="rx-more-list" v-if="show">
                <template v-for="(obj,index) in buttons">
                    <div @click.stop="change(obj)" v-if="index>=defaultQuantity-1" :class="{'rx-disable':obj.disable}" class="rx-more-list-name" :style="[buttonLoading.loading?'opacity: 0.5;':'']">
                        <uv-loading-icon v-if="buttonLoading.key && buttonLoading.key==obj.method"></uv-loading-icon>{{ $i18(obj["i18nJson"] || obj[label]) }}
                    </div>
                </template>
            </div>
            <div class="rx-center-button" :style="[buttonLoading.loading?'opacity: 0.5;':'']">
                <template v-for="(obj,index) in buttons">
                    <div @click.stop="change(obj)" class="rx-name" :class="{'rx-highlight':index==0,'rx-disable':obj.disable}"
                         v-if="showFide(index)">
                        <uv-loading-icon v-if="buttonLoading.key && buttonLoading.key==obj.method"></uv-loading-icon>{{ $i18(obj["i18nJson"] || obj[label]) }}
                    </div>
                </template>
                <div class="rx-name" v-if="buttons.length>defaultQuantity" @click.stop="show=!show">
                    <my-icon type="icon-more1" style="margin-right: 0.2rem"></my-icon>
                    {{ $ti18('rxList.moreBtn', '更多') }}
                </div>
            </div>
        </template>
        <template v-if="styleFid=='subTable'">
            <div class="rx-sub-more-list" v-if="show">
                <template v-for="(obj,index) in buttons">
                    <div @click.stop="change(obj)" v-if="index>=defaultQuantity-1" class="rx-more-list-name" :class="{'rx-disable':obj.disable}" :style="[buttonLoading.loading?'opacity: 0.5;':'']">
                        <uv-loading-icon v-if="buttonLoading.key && buttonLoading.key==obj.method"></uv-loading-icon>{{ $i18(obj["i18nJson"] || obj[label]) }}
                    </div>
                </template>
            </div>
            <div class="rx-sub-table">
                <template v-for="(obj,index) in buttons">
                    <div class="rx-sub-name" :class="{'rx-disable':obj.disable}" @click.stop="change(obj)" v-if="showFide(index)">
                        <my-icon v-if="obj.key=='add'" style="margin-right: 0.5rem" type="icon-jia"></my-icon>
                        <my-icon v-if="obj.key=='delete'" style="margin-right: 0.5rem" type="icon-Vue3_delete"></my-icon>
                        {{ $i18(obj["i18nJson"] || obj[label]) }}
                    </div>
                </template>
                <div class="rx-sub-name" v-if="buttons.length>defaultQuantity" @click.stop="show=!show">
                    <my-icon type="icon-more1" style="margin-right: 0.5rem"></my-icon>
                    {{ $ti18('rxList.moreBtn', '更多') }}
                </div>
            </div>
        </template>
    </div>
</template>

<script>
import {onMounted, onUnmounted} from 'vue';

export default {
    name: "RxButtonList",
    props: {
        localButtons: {
            type: Array,
            default: () => []
        },
        defaultQuantity: {
            type: Number,
            default: 3
        },
        label: {
            type: String,
            default: "name"
        },
        buttonLoading:{
            type: Object,
            default:{
                key:'',
                loading:false
            }
        },
        styleFid:{
            type: String,
            default: "1"
        }
    },
    data() {
        return {
            show: false,
            buttons: []
        }
    },
    created() {
        // #ifdef H5
        onMounted(() => {
            // 在组件挂载后添加事件监听器
            document.addEventListener('click', this.handleOutsideClick);
        });

        onUnmounted(() => {
            // 在组件销毁前移除事件监听器
            document.removeEventListener('click', this.handleOutsideClick);
        });
        // #endif
    },
    methods: {
        showFide(index){
            if(this.buttons.length==this.defaultQuantity || index<this.defaultQuantity-1){
                return true
            }else {
                return false
            }
        },
        handleOutsideClick(event) {
            this.show = false
        },
        change(obj) {
            if(obj.disable){
                return
            }
            this.$emit('change', obj)
        }
    },
    watch: {
        localButtons: {
            handler(val) {
                this.buttons = val;
            },
            deep: true,
            immediate: true
        },
    }
}
</script>

<style scoped>
.rx-sub-center{
    position: relative;
    display: flex;
    align-items: center;
    height: 2.5rem;
    border-top: 1px solid rgba(232, 232, 232, 1);
}
.rx-sub-table{
    display: flex;
    align-items: center;
    width: 100%;
    color: rgba(0, 0, 0, 0.65);
    flex-direction: row-reverse;
}
.rx-sub-name:before{
    content: '|';
    position: absolute;
    right: 0rem;
    color: rgba(232, 232, 232, 1);
}
.rx-sub-name:nth-child(1):before{
    display: none;
}
.rx-sub-name{
    flex: 1;
    font-size: 0.8rem;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}
.rx-more {
    width: 5rem;
    display: flex;
    align-items: center;
}

.rx-center {
    position: relative;
    width: 100%;
    display: flex;
    height: 3rem;
    opacity: 1;
    background: rgba(255, 255, 255, 1);
    align-items: center;
    padding: 0rem 0.5rem;
    box-sizing: border-box;
    box-shadow: 0rem 0rem 0.28rem rgba(42, 89, 163, 0.2);
}

.rx-center-button {
    flex: 1;
    display: flex;
    flex-direction: row-reverse;
    overflow: hidden;
}

.rx-name {
    flex: 1;
    min-height: 2rem;
    border-radius: 0.35rem;
    background: rgba(255, 255, 255, 1);
    border: 1px solid rgba(232, 232, 232, 1);
    overflow: hidden;
    line-height: 1.2rem;
    padding: 0rem 0.5rem;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 0.5rem;
}
.rx-name:first-of-type{
    margin-right: 0rem;
}
.rx-more-list {
    position: absolute;
    bottom: 4.5rem;
    left: 1rem;
    background: white;
    z-index: 99;
    min-width: 6.5rem;
    padding: 0rem 0.5rem;
    opacity: 1;
    display: flex;
    display: flex;
    flex-direction: column;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0rem 0rem 0.43rem rgba(0, 0, 0, 0.1);
    border-radius: 5px;
}
.rx-sub-more-list{
    position: absolute;
    bottom: 3rem;
    left: 0.5rem;
    background: white;
    z-index: 99;
    min-width: 6.5rem;
    opacity: 1;
    display: flex;
    display: flex;
    flex-direction: column;
    background: rgba(255, 255, 255, 1);
    box-shadow: 0rem 0rem 0.43rem rgba(0, 0, 0, 0.1);
    border-radius: 5px;
}

.rx-more-list-name {
    height: 2.5rem;
    padding:0rem 0.5rem;
    line-height: 2.5rem;
    display: flex;
    align-items: center;
    justify-content: left;
}

.rx-highlight {
    color: white;
    border: none;
    background: rgba(66, 133, 244, 1);
}
.rx-disable{
    opacity: 0.5;
}
</style>
